package com.example.w_library.log;

import com.example.w_library.log.base.WLogConfig;
import com.example.w_library.log.base.WLogManager;
import com.example.w_library.log.base.WLogType;
import com.example.w_library.log.priter.WLogPrinter;
import com.example.w_library.log.fromat.WStackTraceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

/**
 * 入口
 */
public class WLog {

    /**
     * 本模块包名
     */
    private static final String W_LOG_PACKAGE;
    static {
        String className = WLog.class.getName();
        W_LOG_PACKAGE = className.substring(0,className.lastIndexOf('.') + 1);
    }

    public static void v(Object... contents){
        log(WLogType.V,contents);
    }

    public static void vt(String tag,Object... contents){
        log(WLogType.V,tag,contents);
    }



    public static void d(Object... contents){
        log(WLogType.D,contents);
    }

    public static void dt(String tag,Object... contents){
        log(WLogType.D,tag,contents);
    }



    public static void i(Object... contents){
        log(WLogType.I,contents);
    }

    public static void it(String tag,Object... contents){
        log(WLogType.I,tag,contents);
    }



    public static void w(Object... contents){
        log(WLogType.W,contents);
    }

    public static void wt(String tag,Object... contents){
        log(WLogType.W,tag,contents);
    }



    public static void e(Object... contents){
        log(WLogType.E,contents);
    }

    public static void et(String tag,Object... contents){
        log(WLogType.E,tag,contents);
    }



    public static void a(Object... contents){
        log(WLogType.A,contents);
    }

    public static void at(String tag,Object... contents){
        log(WLogType.A,tag,contents);
    }



    private static void log(@WLogType.TYPE int type,Object... contents){
        log(type, WLogManager.getInstance().getConfig().getGlobalTag(),contents);
    }

    private static void log(@WLogType.TYPE int type, @NotNull String tag,Object... contents){
        log(WLogManager.getInstance().getConfig(),type,tag,contents);
    }

    private static void log(@NotNull WLogConfig config, @WLogType.TYPE int type, @NotNull String tag, Object... contents){

        StringBuilder sb = new StringBuilder();

        if (!config.enable()){
            return;
        }

        if (config.includeTread()){
            String threadInfo = WLogConfig.THREAD_FORMATTER.format(Thread.currentThread());
            sb.append(threadInfo).append("\n");
        }

        if (config.stackTraceDepth() > 0){
            String stackTrace = WLogConfig.STACK_TRACE_FORMATTER.format(WStackTraceUtil.getCroppedRealStackTrack(new Throwable().getStackTrace(),W_LOG_PACKAGE,config.stackTraceDepth()));
            sb.append(stackTrace).append("\n");
        }

        String body = parseBody(contents,config);
        sb.append("'\n'---LogInfo---'\n'");
        sb.append(body);

        List<WLogPrinter> printers = config.printers() != null ? Arrays.asList(config.printers()) : WLogManager.getInstance().getPrinters();

        if (printers == null){
            //TODO
            return;
        }

        for (WLogPrinter printer : printers) {
            printer.print(config,type,tag,sb.toString());
        }
    }

    private static String parseBody(@NotNull Object[] contents,@NotNull WLogConfig config){

        if (config.injectJsonParser() != null){
            return config.injectJsonParser().toJson(contents);
        }

        StringBuilder sb = new StringBuilder();

        for (Object content : contents) {

            sb.append(content.toString()).append(";");
        }

        if (sb.length() > 0){

            sb.delete(sb.length() -1 ,sb.length());
        }

        return sb.toString();

    }




}
